<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="goEditMenu('')">添加</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:true" onclick="deleteMenu()">删除</a>
<ul id="edit-tree">
</ul>
<div id="menu-edit-dialog"></div>
<script type="text/javascript">
    $(function () {
        $("#edit-tree").tree({
            url: '<%=request.getContextPath()%>/menu.html?act=tree',
            checkbox: true,
            onClick: function (node) {
                goEditMenu(node.id)
            }
        })
    });

    function deleteMenu() {
        var nodes = $('#edit-tree').tree('getChecked');
        var ids = new Array();
        for (var i = 0; i < nodes.length; i++) {
            ids.push(nodes[i].id);
        }
        //alert(ids.join(","))
        $.ajax({
            url: "<%=request.getContextPath()%>/menu.html?act=delete",
            data: {"ids": ids.join(",")},
            method: "post",
            success: function (result) {
                if (result.success) {
                    $('#edit-tree').tree('reload');
                } else {
                    alert(result.message)
                }
            }
        })
    }

    function goEditMenu(id) {
        $("#menu-edit-dialog").dialog({
            href: '<%=request.getContextPath()%>/menu.html?act=go_edit&id=' + id,
            modal: true,
            width: 400,
            height: 300,
            buttons: [{
                text: '保存',
                iconCls: "icon-save",
                handler: function () {
                    $.ajax({
                        url: "<%=request.getContextPath()%>/menu.html?act=edit",
                        data: $("#menu-form").serialize(),
                        method: "post",
                        success: function (result) {
                            if (result.success) {
                                $('#edit-tree').tree('reload');
                                $("#menu-edit-dialog").dialog("close");
                            } else {
                                alert(result.message)
                            }
                        }
                    })
                }
            }, {
                text: '取消',
                handler: function () {
                    $("#menu-edit-dialog").dialog("close");
                }
            }]

        })
    }
</script>
